package com.woniu.product.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu.core.util.JwtUtil;
import com.woniu.core.util.PageInfo;
import com.woniu.core.util.RedisUtil;
import com.woniu.core.util.Result;
import com.woniu.product.client.AccountServerClient;
import com.woniu.product.client.dto.GymAccountDto;
import com.woniu.product.dto.TCourseDto;
import com.woniu.product.dto.TCourseQueryDto;
import com.woniu.product.dto.TCourseTypeQueryDto;
import com.woniu.product.form.*;
import com.woniu.product.model.TCourse;
import com.woniu.product.model.TCourseType;
import com.woniu.product.param.*;
import com.woniu.product.service.TCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *课程
 * @author 吴彦祖
 * @since 2024年04月02日
 */
@RestController
@RequestMapping("/tCourse")
@Api(tags="课程")
public class TCourseController {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private TCourseService tCourseService;

    //新增课程(已测)
    @PostMapping("/insert.gym")
    @ApiOperation("新增课程")
    public Result insert(@Valid @RequestBody TCourseInsertForm form, @RequestHeader String token) throws Exception{
        //取id
        String refreshToken = redisUtil.get(token);
        int gymId = Integer.parseInt(JwtUtil.parseToken(refreshToken,4).get("gymId").toString());
        TCourseInsertParam param = BeanUtil.toBean(form, TCourseInsertParam.class);
        param.setGymId(gymId);
        TCourse tCourse = tCourseService.insert(param);

        return Result.getResult(TCourse.class).setMsg("新增课程成功").setData(tCourse);
    }
    //删除课程
    @GetMapping("/delete.gym/{courseId}")
    @ApiOperation("根据课程id删除课程")
    public Result delete(@PathVariable("courseId")Integer courseId) throws Exception{
        TCourse tCourse = tCourseService.delete(courseId);
        return Result.getResult(TCourse.class).setMsg("删除课程成功").setData(tCourse);
    }
    //根据课程id上架
    @GetMapping("/enableById.gym/{courseId}")
    @ApiOperation("根据课程id上架")
    public Result enableById(@PathVariable("courseId")Integer courseId) throws Exception{
        TCourseChangeStateParam param = new TCourseChangeStateParam();
        param.setCourseId(courseId);
        param.setCourseState(2);
        TCourseDto dto = tCourseService.changeStateById(param);
        return Result.getResult(TCourseDto.class).setMsg("上架成功").setData(dto);
    }
    //根据课程id下架
    @GetMapping("/downById.gym/{courseId}")
    @ApiOperation("根据课程id下架")
    public Result downById(@PathVariable("courseId")Integer courseId) throws Exception{
        TCourseChangeStateParam param = new TCourseChangeStateParam();
        param.setCourseId(courseId);
        param.setCourseState(3);
        TCourseDto dto = tCourseService.changeStateById(param);
        return Result.getResult(TCourseDto.class).setMsg("下架成功").setData(dto);
    }
    //根据课程id完结
    @GetMapping("/okById.gym/{courseId}")
    @ApiOperation("根据课程id完结")
    public Result okById(@PathVariable("courseId")Integer courseId) throws Exception{
        TCourseChangeStateParam param = new TCourseChangeStateParam();
        param.setCourseId(courseId);
        param.setCourseState(4);
        TCourseDto dto = tCourseService.changeStateById(param);
        return Result.getResult(TCourseDto.class).setMsg("完结成功").setData(dto);
    }

    //根据课程id修改课程
    @PostMapping("/changeCourseById.gym")
    @ApiOperation("根据课程id修改课程")
    public Result changeCourseById(@Valid @RequestBody TCourseChangeForm form) throws Exception{
        TCourseChangeParam param = BeanUtil.toBean(form, TCourseChangeParam.class);
        TCourse tCourse = tCourseService.changeCourseById(param);
        return Result.getResult(TCourse.class).setMsg("修改课程成功").setData(tCourse);
    }
    //根据课程id查询课程（已测）
    @GetMapping("/queryCourseById.gym/{courseId}")
    @ApiOperation("根据课程id查询课程")
    public Result queryCourseById(@PathVariable("courseId")Integer courseId) throws Exception{
        TCourseQueryDto tCourse = tCourseService.queryCourseById(courseId);

        return Result.getResult(TCourseQueryDto.class).setMsg("根据课程id查询课程成功").setData(tCourse);
    }
    //查询所有课程/带条件查询
    @PostMapping("/queryAll")
    @ApiOperation("查询所有课程/带条件查询")
    public Result<PageInfo> queryAll(@RequestBody TCourseQueryForm form) throws Exception{
        PageInfo<TCourseQueryDto> info = tCourseService.queryAll(BeanUtil.toBean(form, TCourseQueryParam.class));
        return Result.getResult(PageInfo.class).setMsg("查询所有课程/带条件查询").setData(info);
    }
}

